/*******************************************************************************
 * @project: EveryClass
 * @package: com.burns.everyclass.lunene.test
 * @file: T.java
 * @author: Administrator
 * @created: 2017年6月4日
 * @purpose:
 * 
 * @version: 1.0
 * 
 * Revision History at the end of file.
 * 
 * Copyright 2017 AcconSys All rights reserved.
 ******************************************************************************/

package com.burns.everyclass.lunene.test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;

import com.burns.everyclass.lunene.utils.Indexer;
import com.burns.everyclass.lunene.utils.JarFileFilter;
import com.burns.everyclass.lunene.utils.LuceneConstants;
import com.burns.everyclass.lunene.utils.Searcher;
import com.burns.everyclass.lunene.utils.SystemUtils;

public class LuceneTester {

	List<File> jarFileList = new ArrayList<File>();
	String indexDir = "D:\\Lucene\\Index";
	String dataDir = "D:\\Lucene\\Data";
	Indexer indexer;
	Searcher searcher;

	public static void main(String[] args) {
		LuceneTester tester;
		try {
			tester = new LuceneTester();
			tester.createIndex();
//			tester.search("txt1");
		} catch (IOException e) {
			e.printStackTrace();
//		} catch (ParseException e) {
//			e.printStackTrace();
		}
	}

	private void createIndex() throws IOException {
		//1、获得系统的所有盘符
		File[] files = SystemUtils.getDrivers();
		//2、获得某个目录下所有的jar包
		List<File> jarFileList = new ArrayList<File>();
		File testFile = new File("D:/BaiduNetdiskDownload");
		System.out.println(testFile.listFiles().length);
//		for (File file : files) {
			SystemUtils.getFilesFromDirectory(testFile);
			jarFileList = SystemUtils.getJarFileList();
//		}
		System.out.println("#################################################################");
		System.out.println(jarFileList.size());
		for (File file : jarFileList) {
			System.out.println(file.getName()+">>>>>>>"+file.getAbsolutePath());
		}
		
//		indexer = new Indexer(indexDir);
//		int numIndexed;
//		long startTime = System.currentTimeMillis();
//		numIndexed = indexer.createIndex(dataDir, new JarFileFilter());
//		long endTime = System.currentTimeMillis();
//		indexer.close();
//		System.out.println(numIndexed + " File indexed, time taken: " + (endTime - startTime) + " ms");
	}

	private void search(String searchQuery) throws IOException, ParseException {
		searcher = new Searcher(indexDir);
		long startTime = System.currentTimeMillis();
		TopDocs hits = searcher.search(searchQuery);
		long endTime = System.currentTimeMillis();

		System.out.println(hits.totalHits + " documents found. Time :" + (endTime - startTime));
		for (ScoreDoc scoreDoc : hits.scoreDocs) {
			Document doc = searcher.getDocument(scoreDoc);
			System.out.println("File: " + doc.get(LuceneConstants.FILE_PATH));
		}
		searcher.close();
	}
}

/*******************************************************************************
 * <B>Revision History</B><BR>
 * [type 'revision' and press Alt + / to insert revision block]<BR>
 * 
 * 
 * 
 * Copyright 2017 AcconSys All rights reserved.
 ******************************************************************************/